/*
 * @Author: yao
 * @Date: 2024-10-31 10:23:32
 * @LastEditTime: 2024-11-20 11:06:55
 * @LastEditors: yao
 * @Description:
 * @FilePath: \element-plus-vite-starter-main\src\router\index.ts
 */
import { createRouter, createWebHistory } from "vue-router";
import constantRoutes from "@/router/constant-routes";
import { ElMessageBox } from "element-plus";

const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

export function resetRouter() {
  router.getRoutes().forEach((route) => {
    const { name } = route;
    if (name && !["Login", "Home", "NotFound", "404"].includes(name)) {
      router.hasRoute(name) && router.removeRoute(name);
    }
  });
}

router.onError((error, to) => {
  if (error?.message.includes("Failed to fetch dynamically imported module")) {
    ElMessageBox.confirm("温馨提示", "系统已升级,请刷新", {
      confirmButtonText: "刷新",
      showCancelButton: false,
    }).then(() => {
      window.location.reload();
    });
  } else {
    console.error("router-onError: ", error, to);
  }
});
export default router;
